package com.gimi.cloud.jimi.app.consumer.service.moreInfo.impl;

import com.gimi.cloud.jimi.app.consumer.feign.MoreInfoFeignService;
import com.gimi.cloud.jimi.app.consumer.service.moreInfo.MoreInfoService;
import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.exception.MsgException;
import com.gimi.cloud.jimi.common.utils.JsonUtil;
import com.gimi.cloud.jimi.common.utils.StringUtil;
import com.gimi.cloud.jimi.common.utils.reqhead.Util;
import com.gimi.cloud.jimi.dto.app.moreInfo.*;
import com.gimi.cloud.jimi.dto.userInfo.RdUserInfoDTO;
import com.gimi.cloud.jimi.dto.params.SocialSecurityParam;
import com.gimi.cloud.jimi.dto.params.TaobaoParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @auther loongs
 * @date 2018/1/9  10:05
 */
@Service
public class MoreInfoServiceImpl implements MoreInfoService {
    private MoreInfoFeignService moreInfoFeignService;
    private static Logger logger = LoggerFactory.getLogger(MoreInfoServiceImpl.class);

    @Autowired
    public MoreInfoServiceImpl(MoreInfoFeignService moreInfoFeignService) {
        this.moreInfoFeignService = moreInfoFeignService;
    }

    @Override
    public void getZmInfo(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("获取芝麻信用，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        MoreInfoDTO moreInfoDTO = JsonUtil.jsonToBean(requestData, MoreInfoDTO.class);
        if (StringUtil.isBlank(moreInfoDTO.getOpenId())) {
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        moreInfoDTO.setUserId(StringUtil.toInt(httpServletRequest.getHeader("userId")));
        Util.sendResponse(moreInfoFeignService.getZmInfo(moreInfoDTO), httpServletResponse);
    }

    @Override
    public void taobaoLogin(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("获取淘宝信用，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        TaobaoParams taobaoParams = JsonUtil.jsonToBean(requestData, TaobaoParams.class);
        Util.sendResponse(moreInfoFeignService.taobaoLogin(taobaoParams), httpServletResponse);
    }

    @Override
    public void getSalaryCardInfo(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("获取工资卡信息，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        Util.sendResponse(moreInfoFeignService.getSalaryCardInfo(), httpServletResponse);
    }

    @Override
    public void getDebitcardLoginParameter(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) {
        logger.info("获取蜂巢工资卡登陆参数，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        DebitcardBankcodeDTO debitcardBankcodeDTO = JsonUtil.jsonToBean(requestData,DebitcardBankcodeDTO.class);
        String userId = httpServletRequest.getHeader("userId");
        if (StringUtil.isBlank(userId) || StringUtil.isBlank(debitcardBankcodeDTO.getFcbcBankCode())) {//验证参数合法性
            logger.error("参数错误");
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        debitcardBankcodeDTO.setUserId(Integer.parseInt(userId));
        Util.sendResponse(moreInfoFeignService.getDebitcardLoginParameter(debitcardBankcodeDTO), httpServletResponse);
    }

    @Override
    public void getDebitcardLogin(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) {
        logger.info("获取蜂巢工资卡登陆参数，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        DebitcardBankcodeDTO debitcardBankcodeDTO = JsonUtil.jsonToBean(requestData,DebitcardBankcodeDTO.class);
        String userId = httpServletRequest.getHeader("userId");
        if (StringUtil.isBlank(userId) || StringUtil.isBlank(debitcardBankcodeDTO.getBankNo()) || StringUtil.isBlank(debitcardBankcodeDTO.getPassword()) || StringUtil.isBlank(debitcardBankcodeDTO.getFcbcBankCode()) || StringUtil.isBlank(debitcardBankcodeDTO.getLoginType())) {//验证参数合法性
            logger.error("参数错误");
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        debitcardBankcodeDTO.setUserId(Integer.parseInt(userId));
        Util.sendResponse(moreInfoFeignService.getDebitcardLogin(debitcardBankcodeDTO), httpServletResponse);
    }

    @Override
    public void subDebitcardValidCode(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) {
        logger.info("蜂巢工资卡登验证码提交接口，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        DebitcardLoginDTO debitcardLoginDTO = JsonUtil.jsonToBean(requestData,DebitcardLoginDTO.class);
        String userId = httpServletRequest.getHeader("userId");
        //校验
        if(StringUtil.isBlank(userId) ||StringUtil.isBlank(debitcardLoginDTO.getSessionToken())||StringUtil.isBlank(debitcardLoginDTO.getValidCode()) || StringUtil.isBlank(debitcardLoginDTO.getCodeType())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        debitcardLoginDTO.setUserId(Integer.parseInt(userId));
        Util.sendResponse(moreInfoFeignService.subDebitcardValidCode(debitcardLoginDTO), httpServletResponse);
    }

    @Override
    public void refreshDebitcardValidcode(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) {
        logger.info("刷新蜂巢工资卡登验证码接口，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        DebitcardLoginDTO debitcardLoginDTO = JsonUtil.jsonToBean(requestData,DebitcardLoginDTO.class);
        String userId = httpServletRequest.getHeader("userId");
        //校验
        if(StringUtil.isBlank(userId) ||StringUtil.isBlank(debitcardLoginDTO.getSessionToken()) || StringUtil.isBlank(debitcardLoginDTO.getCodeType())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        debitcardLoginDTO.setUserId(Integer.parseInt(userId));
        Util.sendResponse(moreInfoFeignService.refreshDebitcardValidcode(debitcardLoginDTO), httpServletResponse);
    }

    @Override
    public void getSupportedCreditcardBanks(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("获取蜂巢信用卡支持银行列表，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        CreditcardBankcodeDTO creditcardBankcodeDTO = JsonUtil.jsonToBean(requestData, CreditcardBankcodeDTO.class);
        Util.sendResponse(moreInfoFeignService.getSupportedCreditcardBanks(creditcardBankcodeDTO), httpServletResponse);
    }

    @Override
    public void getCreditcardLoginParameter(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("获取蜂巢信用卡银行登陆参数，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        CreditcardBankcodeDTO creditcardBankcodeDTO = JsonUtil.jsonToBean(requestData,CreditcardBankcodeDTO.class);
        if (StringUtil.isBlank(creditcardBankcodeDTO.getFcbcId())) {
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        Util.sendResponse(moreInfoFeignService.getCreditcardLoginParameter(creditcardBankcodeDTO), httpServletResponse);
    }

    @Override
    public void subCreditcardBasicParam(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("提交蜂巢信用卡基本登录参数，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        CreditcardLoginDTO creditcardLoginDTO = JsonUtil.jsonToBean(requestData, CreditcardLoginDTO.class);
        if (StringUtil.isBlank(creditcardLoginDTO.getBankCode())||StringUtil.isBlank(creditcardLoginDTO.getLoginParameter())) {
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        creditcardLoginDTO.setUserId(StringUtil.toInt(httpServletRequest.getHeader("userId")));
        Util.sendResponse(moreInfoFeignService.subCreditcardBasicParam(creditcardLoginDTO), httpServletResponse);
    }

    @Override
    public void getCreditcardLoginStatus(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("蜂巢信用卡查询登录状态接口，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        CreditcardLoginDTO creditcardLoginDTO = JsonUtil.jsonToBean(requestData, CreditcardLoginDTO.class);
        creditcardLoginDTO.setUserId(StringUtil.toInt(httpServletRequest.getHeader("userId")));
        Util.sendResponse(moreInfoFeignService.getCreditcardLoginStatus(creditcardLoginDTO), httpServletResponse);
    }

    @Override
    public void subMissionVerifiCationCode(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("蜂巢信用卡提交验证码接口，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        CreditcardLoginDTO creditcardLoginDTO = JsonUtil.jsonToBean(requestData, CreditcardLoginDTO.class);
        if (StringUtil.isBlank(creditcardLoginDTO.getSessionToken())||StringUtil.isBlank(creditcardLoginDTO.getDynamicParameter())) {
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        creditcardLoginDTO.setUserId(StringUtil.toInt(httpServletRequest.getHeader("userId")));
        Util.sendResponse(moreInfoFeignService.subMissionVerifiCationCode(creditcardLoginDTO), httpServletResponse);
    }

    @Override
    public void submitDynamicParam(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("蜂巢信用卡刷新验证码接口，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        CreditcardLoginDTO creditcardLoginDTO = JsonUtil.jsonToBean(requestData, CreditcardLoginDTO.class);
        if (StringUtil.isBlank(creditcardLoginDTO.getSessionToken())||StringUtil.isBlank(creditcardLoginDTO.getDynamicParameter())) {
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        creditcardLoginDTO.setUserId(StringUtil.toInt(httpServletRequest.getHeader("userId")));
        Util.sendResponse(moreInfoFeignService.submitDynamicParam(creditcardLoginDTO), httpServletResponse);
    }


    @Override
    public void getFengchaoEmailList(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("获取蜂巢信用卡支持的邮箱列表，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        FengchaoEmailDTO fengchaoEmailDTO = JsonUtil.jsonToBean(requestData, FengchaoEmailDTO.class);
        Util.sendResponse(moreInfoFeignService.getFengchaoEmailList(fengchaoEmailDTO), httpServletResponse);
    }

    @Override
    public void getFengchaoPositionList(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("蜂巢邮箱登录且获取状态，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        FengchaoPositionDTO fengchaoPositionDTO = JsonUtil.jsonToBean(requestData, FengchaoPositionDTO.class);
        Util.sendResponse(moreInfoFeignService.getFengchaoPositionList(fengchaoPositionDTO), httpServletResponse);
    }

    @Override
    public void getFengchaoEmailLogin(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("蜂巢邮箱登录，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        FengchaoEmailDTO fengchaoEmailDTO = JsonUtil.jsonToBean(requestData, FengchaoEmailDTO.class);
        //校验
        if (StringUtil.isBlank(fengchaoEmailDTO.getUserName()) || StringUtil.isBlank(fengchaoEmailDTO.getPassWord()) ||
                StringUtil.isBlank(fengchaoEmailDTO.getFetchLimit()) || StringUtil.isBlank(fengchaoEmailDTO.getPosition())) {
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        fengchaoEmailDTO.setUserId(StringUtil.toInt(httpServletRequest.getHeader("userId")));
        Util.sendResponse(moreInfoFeignService.getFengchaoEmailLogin(fengchaoEmailDTO), httpServletResponse);
    }

    @Override
    public void getFengchaoEmailStatus(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("蜂巢邮箱状态查询接口，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        FengchaoEmailDTO fengchaoEmailDTO = JsonUtil.jsonToBean(requestData, FengchaoEmailDTO.class);
        //校验
        if (StringUtil.isBlank(fengchaoEmailDTO.getAppemailkey())) {
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        fengchaoEmailDTO.setUserId(StringUtil.toInt(httpServletRequest.getHeader("userId")));
        Util.sendResponse(moreInfoFeignService.getFengchaoEmailStatus(fengchaoEmailDTO), httpServletResponse);
    }

    @Override
    public void subFengchaoEmailIndependentPassWord(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("蜂巢邮箱独立密码提交接口，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        FengchaoEmailDTO fengchaoEmailDTO = JsonUtil.jsonToBean(requestData, FengchaoEmailDTO.class);
        //校验
        if (StringUtil.isBlank(fengchaoEmailDTO.getAppemailkey()) || StringUtil.isBlank(fengchaoEmailDTO.getIvdPassword())) {
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        fengchaoEmailDTO.setUserId(StringUtil.toInt(httpServletRequest.getHeader("userId")));
        Util.sendResponse(moreInfoFeignService.subFengchaoEmailIndependentPassWord(fengchaoEmailDTO), httpServletResponse);
    }

    @Override
    public void subFengchaoEmailValidCode(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("蜂巢邮箱登验证码提交接口，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        FengchaoEmailDTO fengchaoEmailDTO = JsonUtil.jsonToBean(requestData, FengchaoEmailDTO.class);
        //校验
        if (StringUtil.isBlank(fengchaoEmailDTO.getAppemailkey()) || StringUtil.isBlank(fengchaoEmailDTO.getValidCode())) {
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        Util.sendResponse(moreInfoFeignService.subFengchaoEmailValidCode(fengchaoEmailDTO), httpServletResponse);
    }


    @Override
    public void getProvidentFundInfoSupportedCityList(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("获取公积金城市登录列表，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        GongjijingDTO gongjijingDTO = JsonUtil.jsonToBean(requestData, GongjijingDTO.class);
        Util.sendResponse(moreInfoFeignService.getProvidentFundInfoSupportedCityList(gongjijingDTO), httpServletResponse);
    }

    @Override
    public void getProvidentFundInfoCityLoginParameter(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("获取公积金地区登录参数，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        GongjijingDTO gongjijingDTO = JsonUtil.jsonToBean(requestData, GongjijingDTO.class);
        if (StringUtil.isBlank(gongjijingDTO.getRegionId())) {
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        Util.sendResponse(moreInfoFeignService.getProvidentFundInfoCityLoginParameter(gongjijingDTO), httpServletResponse);
    }

    @Override
    public void getProvidentFundInfoImage(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("获取公积金验证码图片，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        GongjijingDTO gongjijingDTO = JsonUtil.jsonToBean(requestData, GongjijingDTO.class);
        if (StringUtil.isBlank(gongjijingDTO.getRegionCityId())) {
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        Util.sendResponse(moreInfoFeignService.getProvidentFundInfoImage(gongjijingDTO), httpServletResponse);
    }

    @Override
    public void getProvidentFundInfo(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("登录并获取公积金信息，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        GongjijingDTO gongjijingDTO = JsonUtil.jsonToBean(requestData, GongjijingDTO.class);
        if (StringUtil.isBlank(gongjijingDTO.getLoginParameter())) {
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        gongjijingDTO.setUserId(StringUtil.toInt(httpServletRequest.getHeader("userId")));
        Util.sendResponse(moreInfoFeignService.getProvidentFundInfo(gongjijingDTO), httpServletResponse);
    }

    @Override
    public void getSheBaoCityList(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("获取社保城市列表，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        SocialSecurityParam socialSecurityParam = JsonUtil.jsonToBean(requestData, SocialSecurityParam.class);
        socialSecurityParam.setOperUserId(StringUtil.toInt(httpServletRequest.getHeader("userId")));
        Util.sendResponse(moreInfoFeignService.getSheBaoCityList(socialSecurityParam), httpServletResponse);
    }

    @Override
    public void getSheBaoCityLoginParamer(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("获取社保登录参数，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        SocialSecurityParam socialSecurityParam = JsonUtil.jsonToBean(requestData, SocialSecurityParam.class);
        if (StringUtil.isBlank(socialSecurityParam.getCityId())) {
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        socialSecurityParam.setOperUserId(StringUtil.toInt(httpServletRequest.getHeader("userId")));
        Util.sendResponse(moreInfoFeignService.getSheBaoCityLoginParamer(socialSecurityParam), httpServletResponse);
    }

    @Override
    public void getSheBaoCodeImage(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("获取社保验证码图片，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        SocialSecurityParam socialSecurityParam = JsonUtil.jsonToBean(requestData, SocialSecurityParam.class);
        if (StringUtil.isBlank(socialSecurityParam.getSessionToken())) {
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        socialSecurityParam.setOperUserId(StringUtil.toInt(httpServletRequest.getHeader("userId")));
        Util.sendResponse(moreInfoFeignService.getSheBaoCodeImage(socialSecurityParam), httpServletResponse);
    }

    @Override
    public void getSheBaoLoginAndGrab(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("登录并抓取社保账号，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        SocialSecurityParam socialSecurityParam = JsonUtil.jsonToBean(requestData, SocialSecurityParam.class);
        if (StringUtil.isBlank(socialSecurityParam.getSessionToken()) || StringUtil.isBlank(socialSecurityParam.getArgs()) ||
                StringUtil.isBlank(socialSecurityParam.getImageCode())) {
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        socialSecurityParam.setOperUserId(StringUtil.toInt(httpServletRequest.getHeader("userId")));
        Util.sendResponse(moreInfoFeignService.getSheBaoLoginAndGrab(socialSecurityParam), httpServletResponse);
    }


}
